package com.sc.portal.service.impl;

import com.sc.common.pojo.CommonResult;
import com.sc.common.utils.HttpClientUtil;
import com.sc.common.utils.JsonUtils;
import com.sc.pojo.TbItem;
import com.sc.pojo.TbItemDesc;
import com.sc.pojo.TbItemParamItem;
import com.sc.portal.service.ItemService;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @auther sc
 * @Date 2017/10/27
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Value(value = "${REST_BASE_URL}")
    private String REST_BASE_URL;

    @Override
    public TbItem getItemById(Long itemId) {
        String json = HttpClientUtil.doGet(REST_BASE_URL + "/item/base/" + itemId);
        CommonResult result = CommonResult.formatToPojo(json,TbItem.class);
        return (TbItem) result.getData();
    }

    @Override
    public String getItemDesc(Long itemID) {
        String json = HttpClientUtil.doGet(REST_BASE_URL + "/item/desc/" + itemID);
        CommonResult result = CommonResult.formatToPojo(json,TbItemDesc.class);
        TbItemDesc tbItemDesc = (TbItemDesc) result.getData();
        String desc = tbItemDesc.getItemDesc();
        return desc;
    }

    @Override
    public String getdetail(Long itemId) {
        String json = HttpClientUtil.doGet(REST_BASE_URL + "/item/param/" + itemId);
        CommonResult result = CommonResult.formatToPojo(json,TbItemParamItem.class);
        TbItemParamItem item = (TbItemParamItem) result.getData();
        String paramData = item.getParamData();
        List<Map> maps = JsonUtils.jsonToList(paramData, Map.class);
        StringBuffer sb = new StringBuffer();
        sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n");
        sb.append("     <tbody>\n");
        for(Map param:maps) {
            sb.append("          <tr>\n");
            sb.append("               <th class=\"tdTitle\" colspan=\"2\">"+param.get("group")+"</th>\n");
            sb.append("          </tr>\n");
            //取规格项
            List<Map> object = (List<Map>) param.get("params");
            for (Map map : object) {
                sb.append("          <tr>\n");
                sb.append("               <td class=\"tdTitle\">"+map.get("k")+"</td>\n");
                sb.append("               <td>"+map.get("v")+"</td>\n");
                sb.append("          </tr>\n");
            }
        }
        sb.append("     </tbody>\n");
        sb.append("</table>");
        return sb.toString();
    }
}
